package org.example.controller;

import org.example.Service.ProductService;
import org.example.config.SessionManager;
import org.example.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;


    //查询全部可购买商品
    @RequestMapping("/allProduct")
    public String selectList(Model model) {
        List<Product> list = productService.selectProducts();
        model.addAttribute("list", list);
        return "allProduct";
    }

    //查询该名称的可购买商品
    @RequestMapping("/selectProductByName")
    public String selecProductByName(String product_name, Model model) {
        Product product = productService.selectProductByName(product_name);
        System.out.println(product);
        List<Product> list = new ArrayList<>();
        list.add(product);
        if (product == null) {
            list = productService.selectProducts();
            model.addAttribute("error", "未找到!");
        }
        model.addAttribute("list", list);
        return "allProduct";
    }


    //查询全部可管理商品
    @RequestMapping("/allManageProduct")
    public String selectManageList(Model model) {
        List<Product> list = productService.selectProducts();
        model.addAttribute("list", list);
        return "ManageProduct";
    }

    //查询该名称的可管理商品
    @RequestMapping("/selectManageProductByName")
    public String selectManageProductByName(String product_name, Model model) {
        Product product = productService.selectProductByName(product_name);
        System.out.println(product);
        List<Product> list = new ArrayList<>();
        list.add(product);
        if (product == null) {
            list = productService.selectProducts();
            model.addAttribute("error", "未找到!");
        }
        if (product_name == "") {
            list = productService.selectProducts();
            model.addAttribute("error", "请输入商品编号!");
        }
        model.addAttribute("list", list);
        return "ManageProduct";
    }

    //跳转到添加商品页面
    @RequestMapping("/toInsertProduct")
    public String toInsertProduct() {
        return "insertProduct";
    }

    //添加商品
    @RequestMapping("/insertProduct")
    public String insertProduct(Product product) {
        productService.insertProduct(product);
        return "ManageProduct";
    }

    //跳转到修改商品页面
    @RequestMapping("/toUpdateProduct/{id}")
    public String toUpdateProduct(@PathVariable("id") Integer id, Model model) {
        Product product = productService.selectProductByID(id);
        model.addAttribute("product", product);
        return "updateProduct";
    }

    //修改商品
    @RequestMapping("/updateProduct")
    public String updateProduct(Product product) {
        productService.updateProduct(product);
        return "ManageProduct";
    }

    //删除商品
    @RequestMapping("/deleteProduct/{id}")
    public String deleteProduct(@PathVariable("id") Integer id) {
        productService.deleteProduct(id);
        return "ManageProduct";
    }
}
